package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.Product;
import com.iscc.fake.service.Impl.ProductServiceImpl;
import com.iscc.fake.service.ProductService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/add_product.do")
public class ProductServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, Object> resultMap = new HashMap<>();
        Gson gson =new Gson();

        ProductService productService =new ProductServiceImpl();
        //创建DiskFileItemFactory对象,允许文件上传；
        DiskFileItemFactory factory=new DiskFileItemFactory();
        ServletFileUpload upload=new ServletFileUpload(factory);
        Product product=new Product();
        String path =this.getServletContext().getRealPath("upload");
        System.out.println(path);
        File uploadfile =new File(path);
        if(!uploadfile.exists()){
             uploadfile.mkdirs();
        }
        List<DiskFileItem> dlist =new ArrayList<>();
        try {
            List<FileItem> items = upload.parseRequest(request);
            for(FileItem item:items){
                if(item.isFormField()){
                    String val =new String(item.getString().getBytes("iso-8859-1"),"UTF-8");
                    if("proname".equals(item.getFieldName())){
                        product.setPname(val);
                    }else if ("proprice".equals(item.getFieldName())){
                        product.setPrice(Double.parseDouble(val));
                    }else if ("prosallery".equals(item.getFieldName())){
                        product.setSallery(Double.parseDouble(val));
                    }else if ("protype".equals(item.getFieldName())){
                        product.setType(val);
                    }else if ("prospec".equals(item.getFieldName())){
                        product.setSpec(val);
                    }else if ("prodescribes".equals(item.getFieldName())){
                        product.setDescribes(val);
                    }
                }else{
                    DiskFileItem  fileItem =(DiskFileItem)item;
                    if("procover".equals(item.getFieldName())){
                        System.out.println(fileItem.getName());
                        File coverFile = new File(uploadfile,fileItem.getName());
                        fileItem.write(coverFile);
                        System.out.println(coverFile);
                        product.setPicture("/upload/"+coverFile.getName());
                    }else{
                        dlist.add(fileItem);
                    }
                }
            }

            for (DiskFileItem fileItem : dlist) {
                File srcFile = new File(uploadfile,fileItem.getName());
                fileItem.write(srcFile);
                product.setSrc("/upload/"+srcFile.getName());
            }

            int result =productService.addProduct(product);
            if (result != -1) {
                resultMap.put("code", 200);
                resultMap.put("msg", "添加商品成功");
            } else {
                resultMap.put("code", 201);
                resultMap.put("msg", "添加商品失败");
            }
            response.getWriter().print(gson.toJson(resultMap));
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
